package com.sunny.ynhz.ui.daily.specialcolumn;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.sunny.ynhz.R;
import com.sunny.ynhz.ui.base.AppBaseNoTranslationTitleBarActivity;
import com.sunny.ynhz.ui.index.HotRedFragment;

import butterknife.BindView;

/**
 * Created by luao on 2017/8/20.
 */

public class ListActivity extends AppBaseNoTranslationTitleBarActivity {
    Fragment nowFragment1;
    private HotRedFragment blackFragment;
    private HotRedFragment redFragment;
    @BindView(R.id.rg_ll)
    RadioGroup rg_ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.red_black_activity);
        setTitle("红黑榜");
        blackFragment = new HotRedFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", 8);
        bundle.putString("title","黑榜");
        blackFragment.setArguments(bundle);
        redFragment = new HotRedFragment();
        Bundle bundleRed = new Bundle();
        bundleRed.putInt("id", 7);
        bundleRed.putString("title","红榜");
        redFragment.setArguments(bundleRed);
        swithFragment(redFragment, null);
        rg_ll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.red_fragment) {
                    swithFragment(nowFragment1, redFragment);
                } else {
                    swithFragment(nowFragment1, blackFragment);
                }
            }
        });
    }

    /**
     * @param nowFragment  当前的fragment
     * @param nextFragment 即将切换的fragment
     */
    public void swithFragment(Fragment nowFragment, Fragment nextFragment) {
        nowFragment1 = nextFragment;
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        if (nextFragment == null) {
            fragmentTransaction.add(R.id.ll, nowFragment).commit();
            nowFragment1 = nowFragment;
        } else {
            if (nextFragment.isAdded()) {
                fragmentTransaction.hide(nowFragment).show(nextFragment).commit();
            } else {
                fragmentTransaction.hide(nowFragment).add(R.id.ll, nextFragment).commit();

            }
        }
    }
}
